package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

/**
 * @author ming
 * @create 2022/5/2
 * @description:
 */
public class ThreadTest {
    public static  class MingCall implements Callable{

        @Override
        public Integer call() throws Exception {
            System.out.println("异步线程启动了");
            return 1;
        }
    }

    public  void thread(String[] args) throws ExecutionException, InterruptedException {
        //FutureTask<Integer> task = new FutureTask<Integer>(new MingCall());
        //Integer integer = task.get();
        //System.out.println(integer);
        //new Thread(task).start();
        //ExecutorService pool = Executors.newFixedThreadPool(10);
        //pool.execute();
        //线程池
        /**
         * 七大`参数
         * 1.核心线程数,等待接收异步任务
         * int corePoolSize  核心线程数
*          int maximumPoolSize, 最大线程数
*           long keepAliveTime,存活时间
*            TimeUnit unit,
*            BlockingQueue<Runnable> workQueue,
*            ThreadFactory threadFactory,                           
*            RejectedExecutionHandler handler
         * 
         */
     //   ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();
        

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
        System.out.println("main--------start-------------");
        //CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //    System.out.println("当前线程:" + Thread.currentThread().getId());
        //    int i = 10 / 2;
        //    System.out.println("运行结果" + i);
        //
        //}, service);
        //CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("当前线程:" + Thread.currentThread().getId());
        //    int i = 10 / 0;
        //    System.out.println("运行结果" + i);
        //    return i;
        //}).handle((t,u)->{
        //    if(t!=null){
        //        return t*2;
        //    }
        //    if(u!=null){
        //        return 0;
        //    }
        //    return 0;
        //}).exceptionally(throwable ->{
        //    return -1;
        //});

        //future.whenComplete((result,exception)->{
        //    //虽然能得到异常无法修改返回数据
        //    System.out.println("结果是"+result);
        //    System.out.println("异常是"+exception);
        //}) ;
        //future.exceptionally(throwable -> {
        //    //感知异常同时返回默认只
        //    return -1;
        //});
      //  System.out.println("main-------------end-------"+future.get());
        /**
         * 线程串行话
         * thenRun 不能获取到上一步的执行结果
         * thenRunAsync
         */
        //CompletableFuture.supplyAsync(()->{
        //    System.out.println("当前线程:" + Thread.currentThread().getId());
        //    int i = 10 / 0;
        //    System.out.println("运行结果" + i);
        //    return 1;
        //},service).thenRunAsync(()->{
        //    System.out.println("任务二启动了");
        //},service);
        //CompletableFuture.supplyAsync(()->{
        //    System.out.println("当前线程:" + Thread.currentThread().getId());
        //    int i = 10 / 0;
        //    System.out.println("运行结果" + i);
        //    return 1;
        //},service).thenAcceptAsync((t)->{
        //        System.out.println("任务二启动了");
        //},service).thenApplyAsync((t)->{
        //    System.out.println("任务二启动了");
        //    return 1;
        //},service);
        /**
         * 两个都完成
         */
        //CompletableFuture<String> future01 = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("当前线程" + Thread.currentThread().getId());
        //    int i = 10 / 4;
        //    System.out.println("任务一运行结束");
        //    return i+"";
        //}, service);
        //CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("当前线程" + Thread.currentThread().getId());
        //    try {
        //        Thread.sleep(3000);
        //    } catch (InterruptedException e) {
        //        e.printStackTrace();
        //    }
        //    System.out.println("任务二结束");
        //    return "hello";
        //}, service);
        //future01.runAfterBothAsync(future02,()->{
        //    System.out.println("任务三开始");
        //},service);
        //future01.thenAcceptBothAsync(future02,(t,u)->{
        //    System.out.println("得到之前的结果"+t+u);
        //},service);
        //CompletableFuture<String> stringCompletableFuture = future01.thenCombineAsync(future02, (t, u) -> {
        //    return t + u;
        //}, service);
        //System.out.println("main-----------end"+stringCompletableFuture.get());
        /**
         * 两个任务,只要有一个完成,我们就执行任务3不到感知结果,自己也无返回值
         */
        //future01.runAfterEitherAsync(future02,()->{
        //    System.out.println("任务三开始之前的结果");
        //},service);
        
        //future01.applyToEitherAsync(future02,res->res.toString(),service);
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(()->{
            System.out.println("查询商品信息");
            return "hello.jpg";
        },service);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(()->{
            System.out.println("查询商品属性");
            return "黑色";
        },service);
        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(()->{
            System.out.println("查询商品介绍");
            return "华为";
        },service);
        CompletableFuture<Void> allof = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        allof.join();
        allof.get();//等待所有结果完成
        System.out.println("main---end------");

    }
}
